const { ipcMain } = require('electron')
import { execCmd, WriteConfig, getFileList, compressPng } from './app'

var MainWindow

var SendToRender = function(event, message) {
  MainWindow.webContents.send(event, message)
}

var ArgsTranslate = function(arg) {
  const platform = arg.platform
  const path = arg.basepath
  let isDebug
  let md5Cache
  if (arg.ServerAddr === '正式服') {
    isDebug = 'false'
  } else {
    isDebug = 'true'
  }
  if (arg.platform === 'web-mobile') {
    md5Cache = ';md5Cache=true'
  } else {
    md5Cache = ';md5Cache=false'
  }
  const content = ['--path',
    path,
    '--build',
    'platform=' + platform + ';debug=' + isDebug + md5Cache
  ]
  return content
}

var Register = function(mainWindow) {
  MainWindow = mainWindow
  ipcMain.on('StartBuild', (event, arg) => {
    console.error('StartBuild:', arg) // prints "ping"
    event.sender.send('StartBuild-Response', arg)
    const content = ArgsTranslate(arg)
    var cmd = '/Applications/CocosCreator191.app/Contents/MacOS/CocosCreator'
    execCmd(cmd, content)
    WriteConfig(arg)
  })
  ipcMain.on('StartBuildManifest', (event, arg) => {
    console.error('StartBuildManifest:', arg) // prints "ping"
    var url = ''
    if (arg.ServerAddr === '正式服') {
      url = 'http://dzPokerHotUpRelease.pokersoon.com:8081/hotUpdate/release/android/'
    } else {
      url = 'http://dzPokerHotUpSandbox.dapai2.com/hotUpdate/sandbox/android/'
    }
    const content = (() => {
      const t = []
      t.push(arg.basepath + '/version_generator.js')
      t.push('-v')
      t.push(arg.version)
      t.push('-u')
      t.push(url)
      t.push('-s')
      t.push(arg.basepath + '/build/test/jsb-default')
      t.push('-d')
      t.push(arg.basepath + '/assets/resources')
      return t
    })()
    var cmd = '/usr/local/bin/node'
    execCmd(cmd, content)
    console.log('生成', cmd, content)
  })
  ipcMain.on('StartPackage', (event, arg) => {
    console.error('StartPackage:', arg) // prints "ping"
    var url = ''
    if (arg.ServerAddr === '正式服') {
      url = 'http://dzPokerHotUpRelease.pokersoon.com:8081/hotUpdate/release/android/'
    } else {
      url = 'http://dzPokerHotUpSandbox.dapai2.com/hotUpdate/sandbox/android/'
    }
    const content = (() => {
      const t = []
      t.push(arg.basepath + '/version_generator.js')
      t.push('-v')
      t.push(arg.version)
      t.push('-u')
      t.push(url)
      t.push('-s')
      t.push(arg.basepath + '/build/test/jsb-default')
      t.push('-d')
      t.push(arg.basepath + '/assets/resources')
      return t
    })()
    var cmd = '/usr/local/bin/node'
    execCmd(cmd, content)
    console.log('生成', cmd, content)
  })

  ipcMain.on('ArgsChange', (event, arg) => {

  })
  ipcMain.on('compress', (event, arg) => {
    getFileList(arg, function(files) {
      for (let index = 0; index < files.length; index++) {
        const element = files[index]
        compressPng(element)
      }
      SendToRender('stdout', '\n压缩后---\n')
      getFileList(arg, function() {
        // SendToRender('shellClose', null)
      })
    })
  })
}
export { Register, SendToRender }
